// 队列的基本操作
// 基于数组实现
class Queue {
    constructor(capacity) { // 容量，最多插入的数量比容量少1，因为最后一个留给队尾
        this.array = new Array(capacity);
        this.front = 0;
        this.rear = 0;
    }
    enQueue(element) {
        if ((this.rear + 1) % this.array.length === this.front) {
            throw new Error('队列已满');
        }
        this.array[this.rear] = element;
        this.rear = (this.rear + 1) % this.array.length;
    }
    deQueue() {
        if (this.rear === this.front) {
            throw new Error('队列已空');
        }
        const deQueueElement = this.array[this.front];
        this.front = (this.front + 1) % this.array.length;
        return deQueueElement;
    }
    output() {
        for (let i = this.front; i !== this.rear; i = (i + 1) % this.array.length) {
            console.log(this.array[i]);
        }
    }
}

const queue = new Queue(6);
queue.enQueue(3);
queue.enQueue(5);
queue.enQueue(6);
queue.enQueue(8);
queue.enQueue(1);
console.log(queue);

queue.deQueue();
queue.deQueue();
queue.deQueue();
queue.output();

queue.enQueue(2);
queue.enQueue(4);
queue.enQueue(9);
queue.output();
